package com.zjitc.pigspring.common;

import cn.hutool.core.util.ObjectUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.zjitc.pigspring.bean.dto.Account;
import com.zjitc.pigspring.bean.po.Admin;
import com.zjitc.pigspring.bean.po.Seller;
import com.zjitc.pigspring.bean.po.User;
import com.zjitc.pigspring.exception.ServiceException;
import com.zjitc.pigspring.mapper.AdminMapper;
import com.zjitc.pigspring.service.AdminService;
import com.zjitc.pigspring.service.SellerService;
import com.zjitc.pigspring.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.descriptor.web.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HandlerInterceptor;

import java.net.ResponseCache;
import java.util.Enumeration;

@Slf4j
public class JwtInterceptor implements HandlerInterceptor {

    @Autowired
    private AdminService adminService;

    @Autowired
    private UserService userService;

    @Autowired
    private SellerService sellerService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("我拦截了");

        String token = "";

        if (request.getMethod().equals("OPTIONS")){
            response.setStatus(HttpServletResponse.SC_OK);
            return true;
        }else{
            token = request.getHeader("token");
        }

        if(token==null||"".equals(token)){
            token = request.getParameter("token");
        }

        if(token==null||"".equals(token)){
            throw new ServiceException(Codes.RESPONSE_NOT_LOGIN,Codes.NOT_LOGIN);
        }

        String userId;
        try {
            userId = JWT.decode(token).getAudience().get(0);

        }catch (JWTDecodeException j){
            throw new ServiceException(Codes.RESPONSE_NOT_LOGIN,Codes.NOT_LOGIN);
        }

        String id = userId.split("-")[0];
        String role = userId.split("-")[1];

        if ("admin".equals(role)){
            Admin admin = adminService.getById(Integer.parseInt(id));
            if (admin==null){
                throw new ServiceException(Codes.RESPONSE_NOT_LOGIN,Codes.NOT_LOGIN);
            }
        }else if("user".equals(role)){
            User user = userService.getById(Integer.parseInt(id));
            if (user==null){
                throw new ServiceException(Codes.RESPONSE_NOT_LOGIN,Codes.NOT_LOGIN);
            }
        }else if("seller".equals(role)){
            Seller seller = sellerService.getById(Integer.parseInt(id));
            if (seller==null){
                throw new ServiceException(Codes.RESPONSE_NOT_LOGIN,Codes.NOT_LOGIN);
            }
        }

        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256("mall")).build();

        try{
            jwtVerifier.verify(token);
        }catch (TokenExpiredException j){
            throw new ServiceException(Codes.RESPONSE_NOT_LOGIN,Codes.NOT_LOGIN);
        }
        return true;
    }




}
